package com.usefullc.user.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.usefullc.common.util.BeanUtils;
import com.usefullc.system.controller.BaseController;
import com.usefullc.system.controller.WebResultBuilder;
import com.usefullc.system.entity.Pager;
import com.usefullc.system.entity.WebResult;
import com.usefullc.system.param.IdsParam;
import com.usefullc.system.validator.UsefulValid;
import com.usefullc.user.domain.Position;
import com.usefullc.user.query.PositionQuery;
import com.usefullc.user.service.PositionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author: Sampson
 * @date: 2022-04-15 15:28
 */
@RestController
@RequestMapping(path = "/user/position/")
@Api( tags="职位管理")
public class PositionController extends BaseController {

    @Autowired
    private PositionService positionService;


    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "获取分页列表", response = WebResult.class)
    @GetMapping(path = "list", produces = "application/json")
    public WebResult list(PositionQuery query) {
        Pager<Position> pager = positionService.findByPage(query);
        return WebResultBuilder.successResult(pager);
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "获取所有列表", response = WebResult.class)
    @GetMapping(path = "listAll", produces = "application/json")
    public WebResult listAll(PositionQuery query) {
        List<Position> dataList = positionService.findAll(query);
        return WebResultBuilder.successResult(dataList);
    }

    @ApiOperation(value = "保存职位", response = WebResult.class)
    @PostMapping(path = "save", produces = "application/json")
    public WebResult save(@UsefulValid(save = true) @RequestBody Position position) {
        positionService.save(position);
        return WebResultBuilder.successResult(position);
    }

    @ApiOperation(value = "修改职位", response = WebResult.class)
    @PostMapping(path = "update", produces = "application/json")
    public WebResult update(@UsefulValid(update = true) @RequestBody Position position) {
        Position oldBean = positionService.findById(position.getId());
        BeanUtils.beanCopy(position, oldBean);
        positionService.save(oldBean);
        return WebResultBuilder.successResult(oldBean);
    }


    @ApiOperation(value = "删除职位", response = WebResult.class)
    @PostMapping(path = "del", produces = "application/json")
    public WebResult del(@UsefulValid(del = true) @RequestBody IdsParam idsParam) {
        positionService.deletePhysByIds(idsParam.getIds());
        return WebResultBuilder.successResult();
    }
}
